﻿/*****************************************介绍*****************************************
 * 作者：User
 * 创建时间：2025-09-15 14:51:05
 * 功能：画册视图
**************************************************************************************/

using Game.Framework;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Game.UI.HallPanel
{
    public class GalleryView : MonoBehaviour
    {
        private HallPanelView m_RootView;
        private EGalleryType m_Type;
        private List<GalleryItem> m_Items;
        private Transform m_Parent;

        private void Awake()
        {
            m_Items = new List<GalleryItem>();
        }

        public void Init(HallPanelView rootView)
        {
            m_RootView = rootView;
            m_Parent = rootView.GetRect(HallPanelNotifications.GalleryContent_RECT);
            m_Items = new List<GalleryItem>();
            PoolManager.Instance.CreatePool(HallPanelNotifications.GALLERY_ITEM_POOL, rootView.GetRect(HallPanelNotifications.Item_RECT).gameObject, 1000);
        }

        private void OnEnable()
        {
            m_RootView.GetControl<Toggle>(HallPanelNotifications.LevelSwitch_CTRL).isOn = true;
            //m_Type = GalleryType.Level;
            //StartCoroutine(InitPage());
            //if (m_RootView.GetControl<Toggle>(HallPanelNotifications.LevelSwitch_CTRL).isOn)
            //{
            //}
            //else
            //    m_RootView.GetControl<Toggle>(HallPanelNotifications.LevelSwitch_CTRL).isOn = true;
        }

        public void ClickToggleHandle(string name, bool isOn)
        {
            switch (name)
            {
                case HallPanelNotifications.LevelSwitch_CTRL:
                    if (isOn)
                    {
                        m_Type = EGalleryType.Level;
                        StartCoroutine(InitPage());
                    }
                    break;
                case HallPanelNotifications.LiveSwitch_CTRL:
                    if (isOn)
                    {
                        m_Type = EGalleryType.Live;
                        StartCoroutine(InitPage());
                    }
                    break;
                case HallPanelNotifications.LikedSwitch_CTRL:
                    if (isOn)
                    {
                        m_Type = EGalleryType.Liked;
                        StartCoroutine(InitPage());
                    }
                    break;
            }
        }

        private IEnumerator InitPage()
        {
            Clear();
            int idx = -1;
            foreach (var item in GameDataMgr.Instance.AllLevelsInfo.Values)
            {
                if (item.status != ELevelStatus.Completed) continue;
                switch (m_Type)
                {
                    case EGalleryType.Level:
                        if (item.type != ELevelType.Normal) continue;
                        break;
                    case EGalleryType.Live:
                        if (item.type != ELevelType.Special) continue;
                        break;
                    case EGalleryType.Liked:
                        if (item.collectStatus != ECollectStatus.Collected) continue;
                        break;
                }

                var obj = PoolManager.Instance.Get(HallPanelNotifications.GALLERY_ITEM_POOL);
                obj.transform.SetParent(m_Parent);
                obj.transform.localScale = Vector3.one;
                (obj.transform as RectTransform).anchoredPosition3D = Vector3.zero;
                var galleryItem = obj.GetComponent<GalleryItem>();
                galleryItem.Init(item, this, m_Type);
                m_Items.Add(galleryItem);
                idx++;

                // 设置链接
                if (idx - 1 >= 0)
                {
                    var previous = m_Items[idx - 1];
                    previous.Next = galleryItem;
                    galleryItem.Previous = previous;
                }

                yield return null;
            }
        }

        private void Clear()
        {
            foreach (var item in m_Items)
            {
                item.Clear();
                PoolManager.Instance.Delete(HallPanelNotifications.GALLERY_ITEM_POOL, item.gameObject);
            }
            m_Items.Clear();
        }

        private void OnDisable()
        {
            Clear();
            GameDataMgr.Instance.Save();
            m_RootView.GetControl<Toggle>(HallPanelNotifications.LevelSwitch_CTRL).isOn = false;
            m_RootView.GetControl<Toggle>(HallPanelNotifications.LiveSwitch_CTRL).isOn = false;
            m_RootView.GetControl<Toggle>(HallPanelNotifications.LikedSwitch_CTRL).isOn = false;
        }

        private void OnDestroy()
        {
            PoolManager.Instance.ClearPool(HallPanelNotifications.GALLERY_ITEM_POOL);
        }

        public void DestroyItem(GalleryItem item)
        {
            item.Clear();
            PoolManager.Instance.Delete(HallPanelNotifications.GALLERY_ITEM_POOL, item.gameObject);
            m_Items.Remove(item);
        }
    }

    public enum EGalleryType
    {
        Level,
        Live,
        Liked,
    }
}